package org.apache.jsp.view.carrinho;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import br.com.skpharma.vo.*;
import br.com.skpharma.dao.*;
import java.util.*;

public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {

  private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();

  private static java.util.List _jspx_dependants;

  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.AnnotationProcessor _jsp_annotationprocessor;

  public Object getDependants() {
    return _jspx_dependants;
  }

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
  }

  public void _jspDestroy() {
  }

  public void _jspService(HttpServletRequest request, HttpServletResponse response)
        throws java.io.IOException, ServletException {

    PageContext pageContext = null;
    HttpSession session = null;
    ServletContext application = null;
    ServletConfig config = null;
    JspWriter out = null;
    Object page = this;
    JspWriter _jspx_out = null;
    PageContext _jspx_page_context = null;


    try {
      response.setContentType("text/html; charset=iso-8859-1");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			null, true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\t\r\n");
      out.write("\t\r\n");
      out.write("\t<html>\r\n");
      out.write("\t<head>\r\n");
      out.write("\t<title></title>\r\n");
      out.write("\t<link href=\"../css/css.css\" rel=\"stylesheet\" type=\"text/css\">\r\n");
      out.write("\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"><style type=\"text/css\">\r\n");
      out.write("\t<!--\r\n");
      out.write("\tbody {\r\n");
      out.write("\t\tmargin-left: 0px;\r\n");
      out.write("\t\tmargin-top: 0px;\r\n");
      out.write("\t\tmargin-right: 0px;\r\n");
      out.write("\t\tmargin-bottom: 0px;\r\n");
      out.write("\t}\r\n");
      out.write("\ta:link {\r\n");
      out.write("\t\tcolor: #000000;\r\n");
      out.write("\t\ttext-decoration: none;\r\n");
      out.write("\t}\r\n");
      out.write("\ta:visited {\r\n");
      out.write("\t\tcolor: #000000;\r\n");
      out.write("\t\ttext-decoration: none;\r\n");
      out.write("\t}\r\n");
      out.write("\ta:hover {\r\n");
      out.write("\t\tcolor: #000000;\r\n");
      out.write("\t\ttext-decoration: none;\r\n");
      out.write("\t}\r\n");
      out.write("\ta:active {\r\n");
      out.write("\t\tcolor: #000000;\r\n");
      out.write("\t\ttext-decoration: none;\r\n");
      out.write("\t}\r\n");
      out.write("\t.style1 {\r\n");
      out.write("\t\tcolor: #FFFFFF;\r\n");
      out.write("\t\tfont-weight: bold;\r\n");
      out.write("\t}\r\n");
      out.write("\t-->\r\n");
      out.write("\t</style></head>\r\n");
      out.write("\t\r\n");
      out.write("\t");

	
	ArrayList lista3 = (ArrayList)session.getAttribute("listaItens");
	
	String action = request.getParameter("action");
	
	
	if(request.getParameter("comprar") != null){	
		
		if (session.getAttribute("usuarioLogado") == null){
			
      out.write("\r\n");
      out.write("\t\t\t\t<script language=\"javascript\">\r\n");
      out.write("\t\t\t\t\talert(\"Faça seu login ou cadastre-se!\");\r\n");
      out.write("\t\t\t\t</script>\r\n");
      out.write("\t\t\t");

		} else{
			int i;
			boolean estoque = false;
			DAOPrateleira daoPrateleira = new DAOPrateleira();
			DAOMedicamento daoMedicamento = new DAOMedicamento();
			PrateleiraVO prat = new PrateleiraVO();
			MedicamentoVO med = new MedicamentoVO(); 
			for (i =0; i<lista3.size(); i++){
				
					System.out.println(((ItemVendaVO)lista3.get(i)).getQtde());
					System.out.println(((ItemVO)((ItemVendaVO)lista3.get(i)).getItemVO()).getProdutoVO().getQtde());
					if (((ItemVendaVO)lista3.get(i)).getQtde() <= ((ItemVO)((ItemVendaVO)lista3.get(i)).getItemVO()).getProdutoVO().getQtde()){
							estoque = true;	
						}
					else{
							estoque = false;
							i = lista3.size() + 10;
								
					}
				}
			
			if (estoque == false){
				
      out.write("\r\n");
      out.write("\t\t\t\t<script language=\"javascript\">\r\n");
      out.write("\t\t\t\t\talert(\"Alguns produtos não estão disponíveis na quantidade desejada!\");\r\n");
      out.write("\t\t\t\t</script>\r\n");
      out.write("\t\t\t\t");

			}
			else {
				VendaVO venda = new VendaVO();
				venda.setClienteVO((ClienteVO)session.getAttribute("usuarioLogado"));
				StatusVO status = new StatusVO();
				DAOStatus daoStatus = new DAOStatus();
				daoStatus.carregar(1);
				status = daoStatus.getStatusVO();
				venda.setStatusVO(status);
				venda.setHorarioEntrega(new Date());
				
				//calcula valor total
				float pr = 0;
				int qt = 0;
				for (int j = 0; j<lista3.size(); j++){
					pr = pr + (((ItemVendaVO)lista3.get(j)).getItemVO().getProdutoVO().getPreco() * ((ItemVendaVO)lista3.get(j)).getQtde());
					qt = qt + ((ItemVendaVO)lista3.get(j)).getQtde();
				}
				
				venda.setValorTotal(qt);
				DAOVenda daoVenda = new DAOVenda();
				daoVenda.salvar(venda,lista3);
				
				ArrayList listanova = new ArrayList();
				session.setAttribute("listaItens",listanova);
				
				
				
      out.write("\r\n");
      out.write("\t\t\t\t<script language=\"javascript\">\r\n");
      out.write("\t\t\t\t\talert(\"Sua compra foi efetuada com Sucesso!\");\r\n");
      out.write("\t\t\t\t\twindow.location.href=\"index.jsp\";\r\n");
      out.write("\t\t\t\t</script>\r\n");
      out.write("\t\t\t");

			}
			
		}
	}
	
	
	if(request.getParameter("limparCarrinho") != null){
		ArrayList listaItens = new ArrayList();
		session.setAttribute("listaItens", listaItens);  
	}
	
	if(request.getParameter("action") != null && action.equals("excluirPrat")){
		int codigo = Integer.parseInt(request.getParameter("cod_prod"));
		int i = 0;
		for (i=0; i < lista3.size(); i++){
			if ( ((ItemVO)((ItemVendaVO)lista3.get(i)).getItemVO()).getProdutoVO().getCod() == codigo){
				lista3.remove(i);
			}
		}
	}
	
	
	
      out.write("\r\n");
      out.write("\t\r\n");
      out.write("\t\r\n");
      out.write("\t<body>\r\n");
      out.write("\t\r\n");
      out.write("\t");

	//for (int i = 0; i<lista3.size(); i++){ 
		//if((((ItemVO)((ItemVendaVO)lista3.get(i)).getItemVO()).getProdutoVO()).getCategoriaVO().getCod() == 2){
			
		//}
	//}
	
      out.write("\r\n");
      out.write("\r\n");
      out.write("\t\r\n");
      out.write("\t<table width=\"700\" border=\"0\" align=\"left\" cellpadding=\"5\" cellspacing=\"2\" bordercolor=\"#f2f2f2\" style=\"font-size:12px\">\r\n");
      out.write("\t  <tr>\r\n");
      out.write("\t    <td width=\"42%\" bgcolor=\"#941C1D\"><p class=\"style1\">Modelo: </p>    </td>    \r\n");
      out.write("\t    <td width=\"10%\" bgcolor=\"#941C1D\"><span class=\"style1\">Conteúdo</span></td>\r\n");
      out.write("\t    <td width=\"8%\" bgcolor=\"#941C1D\"><span class=\"style1\">Preço</span></td>\r\n");
      out.write("\t    <td width=\"20%\" bgcolor=\"#941C1D\"><p class=\"style1\">Categoria:</p></td>\r\n");
      out.write("\t    <td width=\"20%\" bgcolor=\"#941C1D\"><p class=\"style1\">Quantidade à Comprar:</p>    </td>\r\n");
      out.write("\t    <td width=\"20%\" bgcolor=\"#941C1D\"><p><strong></strong></p> </td>\r\n");
      out.write("\t  </tr>\r\n");
      out.write("\t\r\n");
      out.write("\t");

	
	for (int i = 0; i<lista3.size(); i++){
		
		if	((((ItemVO)((ItemVendaVO)lista3.get(i)).getItemVO()).getProdutoVO()).getCategoriaVO().getCod() == 2){
		
      out.write("\r\n");
      out.write("\t\t  \t\t<tr>\r\n");
      out.write("\t\t\t\t    <td width=\"42%\">");
      out.print(((PrateleiraVO)((ItemVO)((ItemVendaVO)lista3.get(i)).getItemVO()).getProdutoVO()).getModeloVO().getDs());
      out.write(' ');
      out.write('-');
      out.write(' ');
      out.print(((PrateleiraVO)((ItemVO)((ItemVendaVO)lista3.get(i)).getItemVO()).getProdutoVO()).getModeloVO().getMarcaVO().getDs());
      out.write("</a></td>\r\n");
      out.write("\t\t\t\t    <td width=\"10%\">");
      out.print(((PrateleiraVO)((ItemVO)((ItemVendaVO)lista3.get(i)).getItemVO()).getProdutoVO()).getConteudo());
      out.write("</td>\r\n");
      out.write("\t\t\t\t    <td width=\"8%\">");
      out.print(((PrateleiraVO)((ItemVO)((ItemVendaVO)lista3.get(i)).getItemVO()).getProdutoVO()).getPreco());
      out.write("</td>\r\n");
      out.write("\t\t\t\t    <td width=\"20%\">");
      out.print(((PrateleiraVO)((ItemVO)((ItemVendaVO)lista3.get(i)).getItemVO()).getProdutoVO()).getCategoriaVO().getDs());
      out.write("</td>\r\n");
      out.write("\t\t\t\t    <td width=\"20%\">");
      out.print(((ItemVendaVO)lista3.get(i)).getQtde());
      out.write("</td>\r\n");
      out.write("\t\t\t\t    <td width=\"20%\"><a href=\"index.jsp?action=excluirPrat&cod_prod=");
      out.print(((PrateleiraVO)((ItemVO)((ItemVendaVO)lista3.get(i)).getItemVO()).getProdutoVO()).getCod());
      out.write("\"><img src=\"../../images/excluir.jpg\" width=\"15\" height=\"13\" border=\"0\"></a></td>\r\n");
      out.write("\t\t  \t\t</tr>\r\n");
      out.write("\t\t");
		
		} 
		else{
			
      out.write("\r\n");
      out.write("\t  \t\t<tr>\r\n");
      out.write("\t\t\t    <td width=\"42%\">");
      out.print(((MedicamentoVO)((ItemVO)((ItemVendaVO)lista3.get(i)).getItemVO()).getProdutoVO()).getDs());
      out.write("</a></td>\r\n");
      out.write("\t\t\t    <td width=\"20%\">");
      out.print(((MedicamentoVO)((ItemVO)((ItemVendaVO)lista3.get(i)).getItemVO()).getProdutoVO()).getConteudo());
      out.write("</td>\r\n");
      out.write("\t\t\t    <td width=\"8%\">");
      out.print(((MedicamentoVO)((ItemVO)((ItemVendaVO)lista3.get(i)).getItemVO()).getProdutoVO()).getPreco());
      out.write("</td>\r\n");
      out.write("\t\t\t    <td width=\"20%\">");
      out.print(((MedicamentoVO)((ItemVO)((ItemVendaVO)lista3.get(i)).getItemVO()).getProdutoVO()).getCategoriaVO().getDs());
      out.write("</td>\r\n");
      out.write("\t\t\t    <td width=\"20%\">");
      out.print(((ItemVendaVO)lista3.get(i)).getQtde());
      out.write("</td>\r\n");
      out.write("\t\t\t    <td width=\"20%\"><a href=\"index.jsp?action=excluirPrat&cod_prod=");
      out.print(((MedicamentoVO)((ItemVO)((ItemVendaVO)lista3.get(i)).getItemVO()).getProdutoVO()).getCod());
      out.write("\"><img src=\"../../images/excluir.jpg\" width=\"15\" height=\"13\" border=\"0\"></a></td>\r\n");
      out.write("\t  \t\t</tr>\r\n");
      out.write("\t\t");
	
			
		}
	}
	//}
	
      out.write("\r\n");
      out.write("\t</table>\r\n");
      out.write("\t<center>\r\n");
      out.write("\t  <br>\r\n");
      out.write("\t  <br>\r\n");
      out.write("\t  <br>\r\n");
      out.write("\t  <br>\r\n");
      out.write("\t  <br>\r\n");
      out.write("\t  <br>\r\n");
      out.write("\t  <br>\r\n");
      out.write("\t  <br>\r\n");
      out.write("\t  <p>\r\n");
      out.write("\t   ");

	   		if (lista3.size() == 0){
	   		
	   	
	   
      out.write("   \r\n");
      out.write("\t   \r\n");
      out.write("\t   ");
}else { 
      out.write("\r\n");
      out.write("      <form method=\"get\" action=\"index.jsp\"> \r\n");
      out.write("\t   <input type=\"submit\" value=\"Limpar Carrinho\" name=\"limparCarrinho\" style=\"font:Verdana, Arial, Helvetica, sans-serif; font-size:12px\">   \r\n");
      out.write("\t   \r\n");
      out.write("\t   <input type=\"submit\" value=\"Confirmar Compra\" name=\"comprar\" style=\"font:Verdana, Arial, Helvetica, sans-serif; font-size:12px\">\r\n");
      out.write("      </form>\r\n");
      out.write("\t   ");
} 
      out.write("\r\n");
      out.write("\t  </p>\r\n");
      out.write("\t</center>\r\n");
      out.write("\t\r\n");
      out.write("\t</body>\r\n");
      out.write("\t</html>\r\n");
      out.write("\t\r\n");
    } catch (Throwable t) {
      if (!(t instanceof SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try { out.clearBuffer(); } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}
